`
https://leetcode.cn/problems/permutation-in-string/
`

/**
 * @param {string} s1
 * @param {string} s2
 * @return {boolean}
 */
var checkInclusion = function (s1, s2) {
  const needs = {}
  const window = {}
  for (const c of s1) {
    needs[c] = (needs[c] || 0) + 1
  }

  let left = 0, right = 0
  let count = 0

  while (right < s2.length) {
    const c = s2[right]
    right++
    if (needs[c]) {
      window[c] = (window[c] || 0) + 1
      if (window[c] === needs[c]) {
        count++
      }
    }

    while (right - left >= s1.length) {
      if (count === Object.keys(needs).length) {
        return true
      }

      const d = s2[left]
      left++

      if (needs[d]) {
        if (needs[d] === window[d]) {
          count--
        }
        window[d]--
      }
    }
  }
  return false
};